<?php

namespace app\model;

use think\Model;

class PointsStaging extends Model
{
    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'points_staging';

    /**
     * 积分返利
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function uppoints()
    {
        $weid = weid();
        $pointsConfig =  Config::getconfig('points');
        if (empty($pointsConfig['stagingweek'])) {
            $pointsConfig['stagingweek'] = 1;
        }
        if ($pointsConfig['is_staging'] == 1 && $pointsConfig['stagingpoints'] > 0 && date('w') == $pointsConfig['stagingweek']) {
            $stagingPoints = Points::where('is_staging', 1)
                ->where('prefix', 1)
                ->where('notreceived_points', '>', 0)
                ->select()
                ->toArray();

            if ($stagingPoints) {
                foreach ($stagingPoints as $vo) {
                    $points = 0;
                    if ($vo['previous_points']) {
                        $points = $vo['previous_points'];
                    } else {
                        $points = $vo['points'] * percent_to_num($pointsConfig['stagingpoints']);
                    }

                    if ($pointsConfig['is_increment'] == 1 && $pointsConfig['increment'] > 0) {
                        $points = $points + $points * percent_to_num($pointsConfig['increment']);
                    }

                    if ($points) {
                        if (!PointsStaging::where('points_id', $vo['id'])->where('create_day', date("Y-m-d"))->find()) {
                            $createdata = [
                                'points_id' => $vo['id'],
                                'uid' => $vo['uid'],
                                'order_id' => $vo['order_id'],
                                'points' => $points,
                                'is_staging' => (int) $pointsConfig['is_staging'],
                                'description' => $vo['description'],
                                'create_day' => date("Y-m-d"),
                                'create_time' => time()
                            ];
                            self::create($createdata);

                            Points::where('id', $vo['id'])
                                ->inc('received_points', (int) $points)
                                ->update();

                            Points::where('id', $vo['id'])
                                ->dec('notreceived_points', (int) $points)
                                ->update();

                            Points::where('id', $vo['id'])
                                ->update([
                                    'staging_day' => date("Y-m-d"),
                                    'previous_points' => $points
                                ]);
                            Member::where('id', $vo['uid'])
                                ->inc('points', (int) $points)
                                ->update();
                        }
                    }
                }
            }
        }
    }
}
